{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "`Learn the Basics <intro.html>`_ ||\n",
    "`Quickstart <quickstart_tutorial.html>`_ || \n",
    "`Tensors <tensorqs_tutorial.html>`_ || \n",
    "`Datasets & DataLoaders <data_tutorial.html>`_ ||\n",
    "**Transforms** ||\n",
    "`Build Model <buildmodel_tutorial.html>`_ ||\n",
    "`Autograd <autogradqs_tutorial.html>`_ ||\n",
    "`Optimization <optimization_tutorial.html>`_ ||\n",
    "`Save & Load Model <saveloadrun_tutorial.html>`_\n",
    "\n",
    "Transforms\n",
    "===================\n",
    "\n",
    "Data does not always come in its final processed form that is required for \n",
    "training machine learning algorithms. We use **transforms** to perform some\n",
    "manipulation of the data and make it suitable for training.\n",
    "\n",
    "All TorchVision datasets have two parameters -``transform`` to modify the features and\n",
    "``target_transform`` to modify the labels - that accept callables containing the transformation logic.\n",
    "The `torchvision.transforms <https://pytorch.org/vision/stable/transforms.html>`_ module offers \n",
    "several commonly-used transforms out of the box.\n",
    "\n",
    "The FashionMNIST features are in PIL Image format, and the labels are integers.\n",
    "For training, we need the features as normalized tensors, and the labels as one-hot encoded tensors.\n",
    "To make these transformations, we use ``ToTensor`` and ``Lambda``.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "数据并不总是以训练机器学习算法所需的最终处理形式出现。我们使用转换来对数据进行一些操作并使其适合训练。\n",
    "所有 TorchVision 数据集都有两个参数 -transform修改特征和 target_transform修改标签 - 接受包含转换逻辑的可调用对象。\n",
    "该torchvision.transforms模块提供几种常用的变换开箱。\n",
    "FashionMNIST 特征采用 PIL Image 格式，标签为整数。对于训练，我们需要将特征作为归一化张量，将标签作为独热编码张量。\n",
    "为了进行这些转换，我们使用ToTensor和Lambda。\n",
    "'''\n",
    "'''\n",
    "ToTensor()\n",
    "ToTensor 将 PIL 图像或 NumPyndarray转换为FloatTensor. 并在 [0., 1.] 范围内缩放图像的像素强度值\n",
    "'''\n",
    "'''\n",
    "Lambda 变换\n",
    "Lambda 转换适用于任何用户定义的 lambda 函数。\n",
    "在这里，我们定义了一个函数来将整数转换为单热编码的张量。\n",
    "它首先创建一个大小为 10（我们数据集中的标签数量）的零张量，并调用 scatter_，它value=1在标签给定的索引上分配 a y。\n",
    "'''\n",
    "import torch\n",
    "from torchvision import datasets\n",
    "from torchvision.transforms import ToTensor, Lambda#对于训练，我们需要将特征作为归一化张量，将标签作为独热编码张量。\n",
    "\n",
    "\n",
    "ds = datasets.FashionMNIST(\n",
    "    root=\"data\",\n",
    "    train=True,\n",
    "    download=True,\n",
    "    transform=ToTensor(),\n",
    "    target_transform=Lambda(lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1))\n",
    ")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "ToTensor()\n",
    "-------------------------------\n",
    "\n",
    "`ToTensor <https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.ToTensor>`_ \n",
    "converts a PIL image or NumPy ``ndarray`` into a ``FloatTensor``. and scales \n",
    "the image's pixel intensity values in the range [0., 1.]\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lambda Transforms\n",
    "-------------------------------\n",
    "\n",
    "Lambda transforms apply any user-defined lambda function. Here, we define a function \n",
    "to turn the integer into a one-hot encoded tensor. \n",
    "It first creates a zero tensor of size 10 (the number of labels in our dataset) and calls \n",
    "`scatter_ <https://pytorch.org/docs/stable/tensors.html#torch.Tensor.scatter_>`_ which assigns a \n",
    "``value=1`` on the index as given by the label ``y``.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "yes\n"
     ]
    }
   ],
   "source": [
    "target_transform = Lambda(lambda y: torch.zeros(\n",
    "    10, dtype=torch.float).scatter_(dim=0, index=torch.tensor(y), value=1))\n",
    "print(\"yes\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "--------------\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Further Reading\n",
    "~~~~~~~~~~~~~~~~~\n",
    "- `torchvision.transforms API <https://pytorch.org/vision/stable/transforms.html>`_\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
